/**
 * 
 */
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.FeatureListEditPart;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.TemplateParamContainerFigure;
import com.cea.papyrus.core.figures.TemplateSignatureFigure;
import com.cea.papyrus.core.listener.UmlElementListener;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * A list container accepting Properties.
 * @author dumoulin
 *
 */
public class TemplateParameterListEditPart extends FeatureListEditPart implements EditPart {

	protected UmlElementListener _adapter ;
	/**
	 * Constructor.
	 * Create a list accepting Property.
	 * @param graphnode
	 * @param modelManager
	 */
	static private int channel=30;
	public TemplateParameterListEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode, org.eclipse.uml2.uml.TemplateParameter.class, modelManager);
		_adapter=new UmlElementListener(this);
	}

	
	public void activate()
	{
		if (isActive()) {
			return;
		}
		super.activate();
		//in order to adapt the accelerator with have to refresh by taking account the changement in class
		if((this.getParent() instanceof TemplateSignatureEditPart) &&((TemplateSignatureEditPart)this.getParent()).getUmlElement()!=null){
			((Notifier)((TemplateSignatureEditPart)this.getParent()).getUmlElement()).eAdapters().add(_adapter);

		}
	}

	protected IFigure createFigure() {
		return new TemplateParamContainerFigure();
	}
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.FeatureListEditPart#deactivate()
	 */
	@Override
	public void deactivate() {
		if (!isActive()) {
			return;
		}

		if((this.getParent() instanceof TemplateSignatureEditPart) &&((TemplateSignatureEditPart)this.getParent()).getUmlElement()!=null){
			((Notifier)((TemplateSignatureEditPart)this.getParent()).getUmlElement()).eAdapters().remove(_adapter);

		}
		
		super.deactivate();
	}

	/*
	 * Add editing capabilities
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();


	}
	protected void refreshVisuals() {
		Debug.debug(this,"J'existe",channel);
		TemplateParamContainerFigure f = (TemplateParamContainerFigure) getFigure();
		f.setBackgroundColor(ColorConstants.white);
		IFigure parentFigure = ((GraphicalEditPart) getParent()).getFigure();

		// Shape presentation
		if (parentFigure instanceof IconFigure) {
			return;
		}
		if( elementType==org.eclipse.uml2.uml.TemplateParameter.class){
			if (((TemplateSignatureFigure)parentFigure).getTemplateParameterContent() == null)
				((TemplateSignatureFigure)parentFigure).addtemplateParameterContainer(f) ;
		}
	}
}
